package design.graduation.novelsystem.model.domian;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * @author: 洪少尉
 * @Date: 2020/4/18 19:20
 * @Description:
 */
public class SSHRegisterEntity {
	/**
	 * 连接器
	 */
	private Connection connect;
	/**
	 * 主机
	 */
	private String host;
	/**
	 * 连接端口
	 */
	private int port;
	/**
	 * 编码
	 */
	private Charset charset;
	/**
	 * 用户
	 */
	private String user;
	/**
	 * 密码
	 */
	private String password;

	public SSHRegisterEntity(CentOSEntity centOSEntity) {
		this.host = centOSEntity.getHost();
		this.port = centOSEntity.getPort();
		this.user = centOSEntity.getUser();
		this.charset = centOSEntity.getCharset();
		this.password = centOSEntity.getPassword();
	}

	/**
	 * 登录Centos主机
	 */
	private boolean login() {
		connect = new Connection(host, port);
		try {
			connect.connect();
			return connect.authenticateWithPassword(user,password);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 获取返回信息
	 */
	public String  processStdout(InputStream in) {
		InputStream stdout = new StreamGobbler(in);
		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
			String line;
			while ((line = br.readLine()) != null) {
				buffer.append(line).append("；\n");
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}

	/**
	 * 批量执行
	 * batchexec shell命令
	 */
	public String batchexec(String[] cmd) throws Exception {
		String returnValue = "";
		try {
			// 认证登录信息
			if (this.login()) {
				// 登陆成功则打开一个会话
				Session session = connect.openSession();
				session.execCommand(StringUtils.join(cmd, ";"));
				returnValue = processStdout(session.getStdout());
				if(StringUtils.isEmpty(returnValue))
				{
					returnValue = processStdout(session.getStderr());
				}
			} else {
				System.out.println("连接失败。");
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception("执行命令 " + StringUtils.join(cmd, ";") + " 失败。");
		} finally {
			connect.close();
		}
		return returnValue;
	}
	public String exec(String shell) throws Exception {
		String returnValue = "";
		try {
			// 认证登录信息
			if (this.login()) {
				// 登陆成功则打开一个会话
				Session session = connect.openSession();
				session.execCommand(shell);
				returnValue = processStdout(session.getStdout());
				if(StringUtils.isEmpty(returnValue))
				{
					returnValue = processStdout(session.getStderr());
				}
			} else {
				System.out.println("连接失败。");
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception("执行命令 " + shell + " 失败。");
		} finally {
			connect.close();
		}
		return returnValue;
	}
	public void send(String shell) throws Exception {
		try {
			// 认证登录信息
			if (this.login()) {
				// 登陆成功则打开一个会话
				Session session = connect.openSession();
				session.execCommand(shell);
			} else {
				System.out.println("连接失败。");
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new Exception("执行命令 " + shell + " 失败。");
		} finally {
			connect.close();
		}
	}
}


